/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.QuestionnaireReply;
import com.boxin.ims.modules.wechat.service.QuestionnaireReplyService;

/**
 * 调查问卷回答Controller
 * @author XiaoQian
 * @version 2014-05-09
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/questionnaireReply")
public class QuestionnaireReplyController extends BaseController {

	@Autowired
	private QuestionnaireReplyService questionnaireReplyService;
	
	@ModelAttribute
	public QuestionnaireReply get(@RequestParam(required=false) Long id) {
		if (id != null){
			return questionnaireReplyService.get(id);
		}else{
			return new QuestionnaireReply();
		}
	}
	
	@RequiresPermissions("wechat:questionnaireReply:view")
	@RequestMapping(value = {"list", ""})
	public String list(QuestionnaireReply questionnaireReply, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
        Page<QuestionnaireReply> page = questionnaireReplyService.find(new Page<QuestionnaireReply>(request, response), questionnaireReply); 
        model.addAttribute("page", page);
		return "modules/wechat/questionnaireReplyList";
	}

	@RequestMapping(value = "form")
	public String form(QuestionnaireReply questionnaireReply, Model model,HttpServletRequest request) {
	
		String replyHumanId=request.getParameter("replyHumanId");
		
		List<QuestionnaireReply> questionnaireReplyList=  questionnaireReplyService.findByReplyHumanId(Long.parseLong(replyHumanId));
		model.addAttribute("questionnaireReplyList", questionnaireReplyList);
		return "modules/wechat/questionnaireReplyForm";
	}

	@RequiresPermissions("wechat:questionnaireReply:edit")
	@RequestMapping(value = "save")
	public String save(QuestionnaireReply questionnaireReply, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request) {
		if (!beanValidator(model, questionnaireReply)){
			return form(questionnaireReply, model,request);
		}
		questionnaireReplyService.save(questionnaireReply);
		addMessage(redirectAttributes, "保存调查问卷回答成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/wechat/questionnaireReply/?repage";
	}
	
	@RequiresPermissions("wechat:questionnaireReply:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		questionnaireReplyService.delete(id);
		addMessage(redirectAttributes, "删除调查问卷回答成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/wechat/questionnaireReply/?repage";
	}

}
